<?php

namespace App\Admin\Selectable;

use App\Models\yth\CustomerModel;
use App\Models\yth\CustomerOrderModel;
use Encore\Admin\Grid\Filter;
use Encore\Admin\Grid\Selectable;
use App\Models\DataDictionaryModel;

class CustomerOrderSelect extends Selectable
{
    public $model = CustomerOrderModel::class;

    public function make()
    {
        $this->model()->where(['status'=>4])->orderBy('created_at','desc');

        $this->column('order_sn','订单编号')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $this->column('customer_id', '客户')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : CustomerModel::selectValue('id',$filed);
        })->modal('客户详情', function($model){
            $customer_data = CustomerModel::where(['id'=>$model->customer_id])->first();
            return view('admin.customer.detail',['comments'=> $customer_data]);
        });

//        $this->column('type_id','业务类型')->display(function($filed){
//            return empty($filed) ? config('view.EmptyData') :DataDictionaryModel::CustomerOrderType[$filed];
//        });

        $this->column('shipment_type','发货类型')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') :DataDictionaryModel::ShipmentType[$filed];
        });

        $this->column('document_date','单据日期')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $this->column('delivery_date','交货日期')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $this->column('total_amount','总金额')->display(function($filed){
            return $filed<0 ? 0 : $filed;
        });

//        $this->column('discount','优惠')->display(function($filed){
//            return $filed<0 ? 0 : $filed;
//        });

        $this->column('actual_amount','实付金额')->display(function($filed){
            return $filed<0 ? 0 : $filed;
        });

        $this->column('status','订单状态')->using(DataDictionaryModel::CustomerOrderStatus, '未知')->label([
            0 => 'danger',
            1 => 'success',
            2 => 'info',
            3 => 'warning',
            4 => 'success',
            5 => 'primary',
        ], 'warning');

        $this->column('created_at','操作时间')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        })->sortable();


        $this->filter(function($filter){
            $filter->column(1/3, function ($filter) {
                $filter->like('order_sn','订单编号');
                $filter->where(function ($query) {
                    $query->whereHas('Customer',function($query){
                        $query->where('customer_name','like','%'.$this->input.'%');
                    });
                },'客户名称');
                $filter->between('document_date', '单据日期')->datetime();
            });
            $filter->column(1/3, function ($filter) {
//                $filter->equal('type_id', '业务类型')->select(DataDictionaryModel::CustomerOrderType);
                $filter->equal('order_status', '订单状态')->select(DataDictionaryModel::CustomerOrderStatus);
                $filter->between('delivery_date', '交货日期')->datetime();
                $filter->between('created_at', '时间选择')->datetime();
            });
            $filter->column(1/3, function ($filter) {
                $filter->equal('shipment_type', '发货类型')->select(DataDictionaryModel::ShipmentType);
                $filter->equal('audit_status', '审核状态')->select(DataDictionaryModel::AuditStatus);
            });
            $filter->disableIdFilter();
        });
    }
}